package com.goodfox.web.generic.provide.taglib;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;

import org.springframework.security.access.ConfigAttribute;

import com.goodfox.web.generic.account.entity.Account;
import com.goodfox.web.generic.authority.filter.SecurityMetadataSourceImpl;

public class PowerCode {

	public static String doEncode(String str) throws JspException,UnsupportedEncodingException {
		if(null!=str) {
			return URLEncoder.encode(URLEncoder.encode(str,"UTF-8"), "UTF-8");
		} else {
			return null;
		}
	}
	
	public static Boolean isPower(String openUrl,Account loginAccount)throws JspException{
//		Account loginAccount=(Account)request.getSession().getAttribute("loginAccount");
		Map<String, Collection<ConfigAttribute>> powerMap=SecurityMetadataSourceImpl.resourcesMap;
		boolean isAuthority=false;
		Collection<ConfigAttribute> configList=powerMap.get(openUrl);
		if(configList!=null){
			for(ConfigAttribute config:configList){
				if(loginAccount!=null){
					if(loginAccount.getRole().getRolePower().equals(config.getAttribute())){
						isAuthority=true;
						break;
					}
				}
			}
		}
		return isAuthority;
	}
}
